package sunstar.com.xq.fragment

import android.app.Activity
import android.content.Intent
import android.support.v4.widget.NestedScrollView
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.squareup.picasso.Picasso
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.startActivity
import sunstar.com.xq.R
import sunstar.com.xq.activity.LoginActivity
import sunstar.com.xq.activity.TimeLineActivity
import sunstar.com.xq.adapter.WallImageAdapter
import sunstar.com.xq.databean.UserData
import sunstar.com.xq.presenter.CheckLoginPresenter
import sunstar.com.xq.util.CircleTransform
import sunstar.com.xq.util.UtilBox
import sunstar.com.xq.util.ViewInitUtil
import sunstar.com.xq.view.MySelfFragmentView

/**
 * Created by Sun.Star on 2017/6/17.
 */
class MySelfFragment : LazyFragment(),AnkoLogger,MySelfFragmentView {
    override fun noData() {
        userSign.visibility = View.GONE
        userNic.text = "请登录"
    }

    override fun loadUserData(data: UserData) {
        if(userSign.visibility == View.GONE){
            userSign.visibility = View.VISIBLE
        }
        userSign.text = data.userSing
        userNic.text = "昵称："+data.nickName
        Picasso.with(activity).load(data.userAvater).transform(CircleTransform()).into(userAvater)
    }

    override fun notLogin() {
        activity.startActivity<LoginActivity>()
    }

    override fun login(clazz:Activity) {
        activity.startActivity(Intent(activity,clazz::class.java).putExtra("userData",""))
    }


    var imgList = mutableListOf<String>()
    var timeLineClick:TextView?=null
    var tiezi:TextView?=null
    var repeat:TextView?=null
    var group:TextView?=null
    var collection:TextView?=null
    var friend:TextView?=null
    var setting:TextView?=null
    var wallView:RecyclerView?=null
    var bottomLayout:LinearLayout?=null
    var textbody:LinearLayout?=null
    var userLayout:LinearLayout?=null
    var childScrollView:NestedScrollView?=null
    var nicName:TextView?=null

    val viewInit: ViewInitUtil by lazy {
        ViewInitUtil(thisView)
    }
    val userNic by lazy {
        viewInit.getView<TextView>(R.id.user_nick)
    }
    val userSign by lazy {
        viewInit.getView<TextView>(R.id.user_sign)
    }
    val userAvater by lazy {
        viewInit.getView<ImageView>(R.id.myself_user_avater)
    }
    var presenter = CheckLoginPresenter(this)
    override fun initData() {

    }

    override fun initViewid(): Int = R.layout.my_self_fragment
    override fun initViewCompelte() {
        initImageData()
        initView()
        presenter.checkLoginData(activity)
        setClickListener()
        wallView!!.adapter = WallImageAdapter(activity,imgList)
        wallView!!.layoutManager =  GridLayoutManager(activity,3)
        wallView!!.isNestedScrollingEnabled = false
        val vto = userLayout!!.getViewTreeObserver()
        vto.addOnGlobalLayoutListener {
            bottomLayout!!.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    UtilBox.getWindowHeight(activity)
                    -(UtilBox.getStatusBarHeight(activity)
                    +UtilBox.getVirtualBarHeigh(activity)
                    +userLayout!!.measuredHeight+textbody!!.measuredHeight))
        }
    }

    private fun setClickListener() {
        tiezi!!.setOnClickListener{
            presenter.checkLogin(activity,TimeLineActivity())
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(data:UserData){
        loadUserData(data)
    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    private fun initView() {
        timeLineClick = thisView.findViewById(R.id.myself_time_lin) as TextView
        tiezi = thisView.findViewById(R.id.myself_time_lin) as TextView
        repeat = thisView.findViewById(R.id.myself_time_lin) as TextView
        group = thisView.findViewById(R.id.myself_time_lin) as TextView
        collection = thisView.findViewById(R.id.myself_time_lin) as TextView
        friend = thisView.findViewById(R.id.myself_time_lin) as TextView
        setting = thisView.findViewById(R.id.myself_time_lin) as TextView
        userLayout = thisView.findViewById(R.id.myself_user_layout) as LinearLayout
        textbody = thisView.findViewById(R.id.myself_text_body) as LinearLayout
        bottomLayout = thisView.findViewById(R.id.myselfy_bottom_layout) as LinearLayout
        wallView = thisView.findViewById(R.id.wall_view) as RecyclerView
        childScrollView = thisView.findViewById(R.id.child_scrollview) as NestedScrollView
    }


    private fun initImageData() {
        imgList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498468276&di=3a94920f682278bdace4070e0c925d26&imgtype=jpg&er=1&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201601%2F20%2F20160120110913_d5WUv.jpeg")
        imgList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497873555861&di=b0507401ed83bfae3ef36e05d21bfb61&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201603%2F23%2F20160323003204_HhE4U.png")
        imgList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497873599521&di=3d9309734d3cc3f06697cb6904d8b6da&imgtype=0&src=http%3A%2F%2Fimgs.shougongke.com%2Fcourse%2F16488542%2F379927%2F161106191951_cover.jpg")
        imgList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497873632293&di=195a9ecfe9e9d64696d7b914ffbfe829&imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201510%2F07%2F20151007201228_UtrV5.jpeg")
        imgList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497873632292&di=b15eb3383f005e48269fc9b7b1cf743e&imgtype=0&src=http%3A%2F%2Fcdn.lizhi.fm%2Faudio_cover%2F2016%2F07%2F21%2F30092358205225095.jpg")
        imgList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497873632291&di=0e6dc8651b2b3b92c4190f458267daf7&imgtype=0&src=http%3A%2F%2Fif.topit.me%2Ff%2F14%2Fd7%2F1103947030ccfd714fo.jpg")
    }

}